package com.kkb.hbase.mr.demo1;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 使用TextInputFormat读取文件是一行一行读取，形成key ,value对，key:行偏移量，一般是LongWritable
 * value是一行文本的内容，String类型 对于Hadoop类型就是Text
 * 读取的是text文件，输入参数默认设置LongWritable, Text
 * 输出的是每个单词Text出现的次数IntWritable
 */
public class HxkMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    //定义输出类型
    IntWritable intWritable = new IntWritable(1);
    Text text = new Text();

    /**
     *
     * @param key
     * @param value  表示一行文本内容
     * @param context 上下文对象。可以将数据通过context写到下游
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //得到每一行内容
        String line = value.toString();
        String[] split = line.split(",");
        for(String word : split){
            text.set(word);
            //写数据到下游
            context.write(text,intWritable);
        }
    }
}
